package com.rhb.ca.web.struts.form;

import org.apache.struts.validator.ValidatorForm;
import java.util.*;

/**
 * this class will be iterated by jsp. 
 * This is a subclass of struts's ActionForm it include all model 
 * colletion that fit for query condition
 * 
 * this class always collaborate with the concrete class of ListAction
 * 
 * this class must be configured in struts-config.xml
 * 
 * <form-bean name="listForm" type="com.rhb.struts.form.ListForm" />
 */
public class ListForm extends ValidatorForm {

	private String behavior;
	private String preBehavior;
	private String prePreBehavior;
    private String behavior_history;
	/**
     * the count of all models that fit for query condition
     */
    private int allCount = 0;

    /**
     * current page start sequence
     */
    private int start = 0;

    /**
     * the count of current page models
     */
    private int count = 18;

  
    /**
     * models collection of current page
     */
    private Collection list = new ArrayList();

    public Collection getList() {
        return list;
    }

    public void setList(Collection list) {
        this.list = list;
    }

    public int getCount() {
        return (this.count);
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getAllCount() {
        return (this.allCount);
    }

    public void setAllCount(int allCount) {
        this.allCount = allCount;
    }

    public int getStart() {
        return (this.start);
    }

    public void setStart(int start) {
        this.start = start;
    }

	public int getAllPages(){
		return  (count==0 ? 0 : (int)Math.ceil(allCount/(count+0.0)));
	}

	public int getCurrentPage(){
		return (count==0 ? 0 : (int)Math.ceil(start/(count+0.0))+1);
	}

	public int getStartOfPrevious(){
		return Math.max(start-count, 0);
	}

	public int getStartOfNext(){
		return (start+count)>=allCount ? start : start+count;
	}

	public int getStartOfEnd(){
		return (this.getAllPages()-1)*count;
	}

	public String getBehavior() {
		return behavior;
	}

	public void setBehavior(String behavior) {
		if(behavior.equals(preBehavior)){
			this.behavior = this.preBehavior;
			this.preBehavior = this.prePreBehavior; 
		}else{
			this.prePreBehavior = this.preBehavior;
			this.preBehavior = this.behavior;
			this.behavior = behavior;
		}
		
		/*
		if(this.behavior_history == null){
			this.behavior_history = behavior;
		}else{
			this.behavior_history = this.behavior_history + "," + behavior;
		}
		System.out.println("prePreBehavior: " + this.prePreBehavior);
		System.out.println("preBehavior: " + this.preBehavior);
		System.out.println("behavior: " + this.behavior);
		System.out.println("behavior_history: " + this.behavior_history);
		*/
	}
	
	public String getPreBehavior() {
		return preBehavior;
	}

	public void setPreBehavior(String behavior) {
		preBehavior = behavior;
	}

	public String getBehavior_history() {
		return behavior_history;
	}

	public void setBehavior_history(String behavior_history) {
		this.behavior_history = behavior_history;
	}

	public String getPrePreBehavior() {
		return prePreBehavior;
	}

	public void setPrePreBehavior(String prePreBehavior) {
		this.prePreBehavior = prePreBehavior;
	}

}
